﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.Framework.Common;
using Wwtfly.BBS.BackModels.Models.Users;
using Wwtfly.BBS.BackDAL.Users;
using Wwtfly.LoggerUtility;
using Wwtfly.BBS.Enums.Common;
using System.IO;
using System.Web;
using Wwtfly.BBS.BackModels.Models.Users.Right;

namespace Wwtfly.BBS.BackBLL.Users
{
    /// <summary>
    /// 用户 信息
    /// </summary>
    public class UsersBLL
    {
               /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="pmusers">pmusers实体</param>
        /// <returns></returns>
        public static bool UpdatePwd(UsersModel pmusers)
        {

            try
            {
                return UsersDAL.UpdatePwd(pmusers);
            }
            catch (Exception ex)
            {
                 LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = " bool UpdatePwd(UsersModel pmusers)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "修改密码.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
                 return false;
            }
        }
        /// <summary>
        /// 分页 获取用户的相关信息
        /// </summary>
        /// <param name="pageParams">分页参数</param>
        /// <param name="searchModel">用户搜索 实体类</param>
        /// <returns></returns>
        public static PagedList<UsersModel> GetUsersInfoPageList(PageParams pageParams, UsersSearchModel searchModel)
        {
            try
            {
                return UsersDAL.GetUsersInfoPageList(pageParams, searchModel);
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "GetUsersInfoPageList",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "分页 获取用户的相关信息.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
                return PagedList<UsersModel>.GetDefaultPageList<UsersModel>();
            }
        }

        /// <summary>
        /// 根据 用户名 获取用户的相关信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static UsersModel GetUsersInfo(string UserName)
        {
            try
            {
                UsersModel um= UsersDAL.GetUsersInfo(UserName);
                if (um.Id > 0)
                {
                    um.UARoleInfoList = GetUARoleInfoList(um.Id);
                    //具有 前台超级,和后台级的 则 为超级管理员
                    var role1 = um.UARoleInfoList.FirstOrDefault(m =>
                         m.RoleCode == SuperManEnum.BACKBBS_SUPERMAN.ToString()
                         && m.IsSystemRole == true
                         );
                    var role2 = um.UARoleInfoList.FirstOrDefault(m =>
                         m.RoleCode == SuperManEnum.FOREBBS_SUPERMAN.ToString()
                         && m.IsSystemRole == true
                         );
                    if (role1 != null && role2!=null)
                    {
                        um.IsSuperMan = true;
                    }
                    else
                    {
                        um.IsSuperMan = false;
                    }
                    um.UARightInfoList = GetUARightInfoList(um.Id);
                }
                return um;
            }
            catch (Exception ex)
            {

                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "GetUsersInfo",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "根据 用户名 获取用户的相关信息.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
                return null;
            }
        }

        /// <summary>
        /// 获取菜单 列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="menuType"></param>
        /// <returns></returns>
        public static IList<UAMenuInfoModel> GetUAMenuInfoList(int userId, ModuleTypeEnum menuType)
        {
            try
            {
                return UsersDAL.GetUAMenuInfoList(userId, menuType.ToString());
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "GetUAMenuInfoList",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "获取菜单 列表.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
               
            }
            return null;
        }

        
        // 获取 用户角色 集合
        /// <summary>
        ///  获取 用户角色 集合
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static IList<UARoleInfoModel> GetUARoleInfoList(int userId)
        {
            try
            {
                return UsersDAL.GetUARoleInfoList(userId);
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "GetUARoleInfoList",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "获取 用户角色 集合.出错",
                    ProgramName = "Wwtilfy.BBS"
                });

            }
            return new List<UARoleInfoModel>();
        }

         //获取 用户权限 集合
        /// <summary>
        ///  获取 用户权限 集合
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static IDictionary<string, int> GetUARightInfoList(int userId)
        {
            try
            {
                return UsersDAL.GetUARightInfoList(userId);
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "GetUARightInfoList",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "获取 用户权限 集合.出错",
                    ProgramName = "Wwtilfy.BBS"
                });

            }
            return new Dictionary<string, int>(0);
        }
        
        /// <summary>
		/// 添加新的pmusers
		/// </summary>
		/// <param name="pmusers">pmusers实体</param>
		/// <returns></returns>
        public static int Add(UsersModel pmusers)
        {
            try
            {
                int var=UsersDAL.Add(pmusers);
                return var;
            }
            catch (Exception ex)
            {
                
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "Add(UsersModel pmusers)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "添加用户.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }
            return -1;
        }

            /// <summary>
        /// 修改个人资料
        /// </summary>
        /// <param name="pmusers"></param>
        /// <returns></returns>
        public static bool UpdateUserMsg(UsersModel pmusers)
        {
            try
            {
                return UsersDAL.UpdateUserMsg(pmusers);
            }
            catch (Exception ex)
            {
                  LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = " UpdateUserMsg(UsersModel pmusers)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "修改 用户修改个人资料.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
                  return false;
            }
        }

        /// <summary>
        /// 修改 用户
        /// </summary>
        /// <param name="pmusers">pmusers实体</param>
        /// <returns></returns>
        public static bool Update(UsersModel pmusers)
        {
            try
            {
                return UsersDAL.Update(pmusers);
            }
            catch (Exception ex)
            {

                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "Update(UsersModel pmusers)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "修改 用户.出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }
            return false;
        }

        /// <summary>
        /// 获取用户信息...
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static UsersModel GetInfo(int userId)
        {
            try
            {
                return UsersDAL.GetInfo(userId);
            }
            catch (Exception ex)
            {

                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "UsersBLL",
                    FuncCode = "GetInfo",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "获取用户信息....出错",
                    ProgramName = "Wwtilfy.BBS"
                });
                return null;
            }
        }
       
    }
}
